<?PHP
 
class Admin_vege_Controller extends controller_admin{
	function __construct(){
		parent::__construct();
	 	$this->assign('nav','菜品管理');
		$this->assign('menuid1','6');
		$this->assign('menuid2','1'); 
		//权限判断
		factory::module('admin')->authSys('cai');
		
	}

	
	
	
	##################################
	/* 种类维护  */
	##################################
	function type__(){
		//权限判断
		 
	 	$this->assign('nav','菜品管理 - 菜品类别'); 
		$db=factory::getDBO();
		$this->assign('rs',$db->loadassoclist('select * from `t_vegetype` order by show_index asc'));	
	}
	/* 添加类别 */
	function addtype__(){
		//权限判断
		 
		$_POST=format::addslash_deep($_POST);
		$db = factory::getDBO();
		$max = $db->loadresult('select max(show_index) from t_vegetype ') + 1;
		$rs = array(
			'type_name'=>$_POST['type_name'],
			'show_index'=>$max
		);
		if($db->insert('vegetype',$rs)){
			exit('{success:1}');
		}else{
			exit('系统繁忙，请稍后重试！');
		}
	}
	/* 删除类别 */
	function deltype__(){
		//权限判断
		 
		$_POST=format::addslash_deep($_POST);
		$db = factory::getDBO();
		$id = $_POST['id'];
		if( $db->loadresult('select count(*) from `t_vege` where type_id="'.$id.'" ') > 0 ){
			exit('操作失败，该类别下已经存在菜品！');
		}	
		if($db->query('delete from `t_vegetype` where id="'.$id.'" ')){
			exit('{success:1,msg11:"删除成功！"}');
		}else{
			exit('系统繁忙，请稍后重试！');
		} 
	}	
	/* 保存类别修改 */
	function mdifytype__(){
		//权限判断
		 
		$objs = format::addslash_json(json_decode( stripslashes($_POST['objs']) ));

		$db = factory::getDBO();
		if($db->updatelist('vegetype',$objs)){
			exit('{success:1,msg:"保存成功！"}');
		}else{
			exit('系统繁忙，请稍后重试！');
		}
	}	
	



	
	##################################
	/* 品牌维护  */
	##################################
	function logo__(){
		//权限判断
		 
	 	$this->assign('nav','菜品管理 - 蔬菜品牌'); 
		$db=factory::getDBO();
		$this->assign('rs',$db->loadassoclist('select * from `t_vegelogo` order by show_index asc'));	
	}
	/* 添加类别 */
	function addlogo__(){
		$_POST=format::addslash_deep($_POST);
		$db = factory::getDBO();
		$max = $db->loadresult('select max(show_index) from t_vegelogo ') + 1;
		$rs = array(
			'logo_name'=>$_POST['logo_name'],
			'show_index'=>$max
		);
		if($db->insert('vegelogo',$rs)){
			exit('{success:1}');
		}else{
			exit('系统繁忙，请稍后重试！');
		}
	}
	/* 删除类别 */
	function dellogo__(){
		//权限判断
		 
		$_POST=format::addslash_deep($_POST);
		$db = factory::getDBO();
		$id = $_POST['id'];
		if( $db->loadresult('select count(*) from `t_vege` where logo_id="'.$id.'" ') > 0 ){
			exit('操作失败，该品牌下已经存在菜品！');
		}	
		if($db->query('delete from `t_vegelogo` where id="'.$id.'" ')){
			exit('{success:1,msg11:"删除成功！"}');
		}else{
			exit('系统繁忙，请稍后重试！');
		} 
	}	
	/* 保存类别修改 */
	function modifylogo__(){
		//权限判断
		 
		$objs = format::addslash_json(json_decode( stripslashes($_POST['objs']) ));

		$db = factory::getDBO();
		if($db->updatelist('vegelogo',$objs)){
			exit('{success:1,msg:"保存成功！"}');
		}else{
			exit('系统繁忙，请稍后重试！');
		}
	}	
		
	
	
	##################################
	/*    */
	##################################
	function index__(){
	   
		//权限判断
		 
	 	$this->assign('nav','菜品管理'); 
		$db=factory::getDBO();
		$this->assign('rs',$db->loadassoclist('select * from `t_vegetype` order by show_index asc'));	
		$this->assign('logos',$db->loadassoclist('select * from `t_vegelogo` order by show_index asc'));	
	}
	function index_list__(){
		//权限判断
		 
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			#得到传递过来的参数
			$_POST=format::addslash_deep($_POST);
			if(!isset($_POST['start']))			$_POST['start']			= 0;
			if(!isset($_POST['limit']))			$_POST['limit']			= 10;
			if(!isset($_POST['sort']))				$_POST['sort']			= 'a.id';
			if(!isset($_POST['dir']))				$_POST['dir']				= 'DESC';
			if(!isset($_POST['keywords']))   $_POST['keywords']  = '';  
			if(!isset($_POST['type_id']))   $_POST['type_id']  = ''; 
			if(!isset($_POST['logo_id']))   $_POST['logo_id']  = '';
			if(!isset($_POST['publishs']))    $_POST['publishs']  = '';  
  	  #查询条件
			$sql_where = ' where    a.deleted=0 '.func::speedsearch($_POST['keywords'],array( 'a.name','a.price', 'a.code','b.type_name' ));
			if($_POST['logo_id'] != ''){
				$sql_where .= ' and a.logo_id="'.$_POST['logo_id'].'" ';
			}
			if($_POST['type_id'] != ''){
				$sql_where .= ' and a.type_id="'.$_POST['type_id'].'" ';
			}
			if($_POST['sale']!=''){
				$sql_where .= ' and a.sale= "'.$_POST['sale'].'"';	
			}
			#排序信息
			$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
			#先查总数 
			$sql = "
			select 
			count(a.id) 
			from `t_vege` a    left join t_vegetype b on a.type_id=b.id  
			".$sql_where ;  
			$db = factory::getDBO();
			$rows = $db->LoadResult($sql); 
			if($rows){
				#检索出具体数据
				$sql = "select
					a.*,
					b.type_name,
					c.logo_name
				from `t_vege` a  left join t_vegetype b on a.type_id=b.id   left join t_vegelogo c on a.logo_id=c.id  
				".$sql_where.$sql_order;
				$rs = $db->LoadAssocList($sql);
				#输出结果
				echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
			}else{
				echo '({"total":"0","results":[]})';
			}
			exit();
		}
	}
	# 添加
	public function add__(){
		//权限判断
		 
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$_POST=format::addslash_deep($_POST);
	 		
			
		//	debug::show($_POST);
			
			$db = factory::getDBO();
			$db->begin();
			//主表  
	 		
			if($db->loadresult('select count(*) from t_vege where deleted=0 and code="'.$_POST['code'].'" ') > 0 ){
				url::msgbox('操作失败！,已经存在此菜品编号！');
			}
			if($db->loadresult('select count(*) from t_vege where deleted=0 and name="'.$_POST['name'].'" ') > 0 ){
				url::msgbox('操作失败！,已经存在此菜品名称！');
			}
			
			$vege_id = $db->insert('vege',array(
				'logo_id'=>$_POST['logo_id'],
				'type_id'=>$_POST['type_id'],
				'name'=>$_POST['name'],
				'price'=>$_POST['price'],
				'pack'=>$_POST['pack'],
				'code'=>$_POST['code'],
				'maxnum'=>$_POST['maxnum'], 
				'remark'=>$_POST['remark'], 
				'sale'=>$_POST['sale']
			));
			if(!$vege_id){
				url::msgalert('操作失败！');
			}

//debug::show(factory::getcfg('vegeimgpath'),$_FILES);
			# 首页图片显示文件分析
			if($_FILES['pic']['name']){
				$UploadFiles = uploadfile::singleton();
				# 做一些大小类型设置 
				$UploadFiles->set_base_directory(factory::getcfg('vegeimg_path'));
				$UploadFiles->set_size(factory::getcfg('vegeimg_maxsize'));
				$UploadFiles->set_extention(factory::getcfg('vegeimg_type')); 
				$UploadFiles->set_file_type($_FILES['pic']['type']);
				$UploadFiles->set_file_name(rand(100000,999999).'.'.preg_replace('/.*\.(.*[^\.].*)*/iU','\\1',$_FILES['pic']['name']));
				$UploadFiles->set_file_size($_FILES['pic']['size']);
				$UploadFiles->set_upfile($_FILES['pic']['tmp_name']);
				$UploadFiles->set_datetime('_'.rand(1,100).'_');
				$UploadFiles->save();
				$db->update('vege',array(
					'id'=>$vege_id,
					'pic'=>$UploadFiles->get_file_path()
				));
			}
			$db->commit();
			url::msgalert('添加成功！',url::build('admin_vege','index')); 
		}
		$db=factory::getDBO();
		$this->assign('types',$db->loadassoclist('select * from `t_vegetype` order by show_index asc','id'));
		$this->assign('logos',$db->loadassoclist('select * from `t_vegelogo` order by show_index asc'));
	}
	
   
	/**
	 * 删除
	 */
	public function  del__(){
		//权限判断
		 
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			exit('暂停菜品删除功能！');
			$idarr = format::addslash_json(json_decode( stripslashes($_POST['idarr']) ));
		
			$db = factory::getDBO(); 
			$db->begin();
			foreach($idarr as $id){
				
				if( $db->loadresult('select count(*) from `t_sendfen_vege` where vege_id="'.$id.'" ') > 0 ){
					exit('操作失败，该菜品已经存在配送批次！');
				}	
				if( $db->loadresult('select count(*) from `t_send_vege` where vege_id="'.$id.'" ') > 0 ){
					exit('操作失败，该菜品已经存在配送批次！');
				}
				
				# 增加套餐使用检测
				
				if( $db->loadresult('select count(a.id) from `t_combo_item` a left join t_combo b on a.combo_id=b.id where a.vege_id="'.$id.'" and b.deleted=0 ') > 0 ){
					exit('操作失败，该菜品已经被固定或组合套餐使用！');
				}
				
				
				$delete = $db->query("update t_vege set deleted=1 where id=".$id);
				if($delete){ 
					 //exit('{success:1,msg:"操作成功！"}'); 
				}else{
					exit('系统繁忙，请稍后再试！');
				}	 
			}
			$db->commit();
			exit('{success:1,msg:"操作成功！"}'); 
		}
		exit();
	}	
	
	
	
	# 修改
	public function  modify__(){  
		//权限判断
		  
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	 
			$_POST=format::addslash_deep($_POST); 
			$db = factory::getDBO(); 
			
			if($db->loadresult('select count(*) from t_vege where deleted=0 and id<>"'.$_POST['id'].'" and code="'.$_POST['code'].'" ') > 0 ){
				url::msgbox('操作失败！,已经存在此菜品编号！');
			}		
			if($db->loadresult('select count(*) from t_vege where deleted=0 and id<>"'.$_POST['id'].'" and name="'.$_POST['name'].'" ') > 0 ){
				url::msgbox('操作失败！,已经存在此菜品名称！');
			}			
			
			
			$db->begin(); 
			$rs_update = array(
				'id'=>$_POST['id'],
				'logo_id'=>$_POST['logo_id'],
				'type_id'=>$_POST['type_id'],
				'name'=>$_POST['name'],
				'price'=>$_POST['price'],
				'pack'=>$_POST['pack'],
				'code'=>$_POST['code'],
				'maxnum'=>$_POST['maxnum'], 
				'remark'=>$_POST['remark'],
				'sale'=>$_POST['sale']
			); 
			if(!isset($_POST['del_pic'])){ 
			}else{
				$rs_update['pic'] = '';
			}	 
			//主表
			if(!$db->update('vege',$rs_update)){
				url::msgalert('操作失败！');
			}
			
			# 首页图片显示文件分析
			if($_FILES['pic']['name']){
				$UploadFiles = uploadfile::singleton();
				# 做一些大小类型设置 
				$UploadFiles->set_base_directory(factory::getcfg('vegeimg_path'));
				$UploadFiles->set_size(factory::getcfg('vegeimg_maxsize'));
				$UploadFiles->set_extention(factory::getcfg('vegeimg_type')); 
				$UploadFiles->set_file_type($_FILES['pic']['type']);
				$UploadFiles->set_file_name(rand(100000,999999).'.'.preg_replace('/.*\.(.*[^\.].*)*/iU','\\1',$_FILES['pic']['name']));
				$UploadFiles->set_file_size($_FILES['pic']['size']);
				$UploadFiles->set_upfile($_FILES['pic']['tmp_name']);
				$UploadFiles->set_datetime('_'.rand(1,100).'_');
				$UploadFiles->save();
				$db->update('vege',array(
					'id'=>$_POST['id'],
					'pic'=>$UploadFiles->get_file_path()
				));
			}
			 
			$db->commit();
			url::msgalert('修改成功！',url::build('admin_vege','index')); 
		}
		$this->assign('menuid1','4');
		$this->assign('nav','菜品管理 - 编辑');   
		$_GET=format::addslash_deep($_GET);
		$id = intval($_GET['id']);
		$db = factory::getDBO();
		$rs = $db->loadassoc('select a.* from `t_vege` a where a.deleted=0 and a.id="'.$id.'" ');
		if(!$rs) url::msgBox('没有查到任何记录，请重新操作！','-1');
		$this->assign('rs',$rs);
		$this->assign('types',$db->loadassoclist('select * from `t_vegetype` order by show_index asc'));
		$this->assign('logos',$db->loadassoclist('select * from `t_vegelogo` order by show_index asc'));
	}
}
?>